package com.rules.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.rules.admin.entity.Rule;
import com.rules.admin.entity.RuleAssembly;
import com.rules.admin.entity.RuleVariable;
import com.rules.admin.service.RuleAssemblyService;
import com.rules.admin.service.RulePackageService;
import com.rules.admin.service.RuleService;
import com.rules.admin.service.RuleVariableService;
import com.rules.admin.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@RequestMapping({"/ruleAssembly"})
@Controller
public class RuleAssemblyController {
   @Autowired
   private RuleAssemblyService ruleAssemblyService;
   @Autowired
   private RuleVariableService ruleVariableService;
   @Autowired
   private RuleService ruleService;
   @Autowired
   private RulePackageService rulePackageService;

   @RequestMapping(
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public IPage<RuleAssembly> queryPage(Long rulePackageId, String name, IPage<RuleAssembly> page) {
      return this.ruleAssemblyService.queryPage(rulePackageId, name, page);
   }

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> addRuleAssembly(@RequestBody RuleAssembly ruleAssembly) {
      List<RuleAssembly> assemblies = this.ruleAssemblyService.getRuleAssemblyByPackageId(ruleAssembly.getPackageId());
      if (assemblies.stream().filter((assembly) -> {
         return assembly.getName().equals(ruleAssembly.getName());
      }).count() > 0L) {
         return ResultUtils.getFaildResultData(new String[]{"name不能重复"});
      } else {
         this.ruleAssemblyService.create(ruleAssembly);
         return ResultUtils.getSuccessResultData();
      }
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public RuleAssembly getRuleAssembly(@PathVariable("id") Long id) {
      return this.ruleAssemblyService.getById(id);
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateRuleAssembly(@RequestBody RuleAssembly ruleAssembly) {
      this.ruleAssemblyService.update(ruleAssembly);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteRuleAssembly(@PathVariable("id") Long id) {
      List<RuleVariable> assmblyList = this.ruleVariableService.getByScopeAndScopeId(2, id);
      if (!assmblyList.isEmpty()) {
         return ResultUtils.getFaildResultData(new String[]{"存在规则集变量，请确认！"});
      } else {
         List<Rule> ruleList = this.ruleService.getRuleListByAssemblyId(id);
         if (!ruleList.isEmpty()) {
            return ResultUtils.getFaildResultData(new String[]{"存在子规则，请确认！"});
         } else {
            this.ruleAssemblyService.delete(id);
            return ResultUtils.getSuccessResultData();
         }
      }
   }

   @RequestMapping(
      value = {"/copy"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public List<RuleAssembly> getRuleAssemblyByPackageId(Long id) {
      return this.ruleAssemblyService.getRuleAssemblyByPackageId(id);
   }

   @RequestMapping(
      value = {"/copyRuleAssembly"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map copyRuleAssembly(Long id) {
      RuleAssembly ruleAssembly = this.ruleAssemblyService.getById(id);
      ruleAssembly.setId((Long)null);
      ruleAssembly.setCreateTime(new Date());
      ruleAssembly.setName(ruleAssembly.getName() + "" + (int)(Math.random() * 90.0D));
      ruleAssembly.setPriority(this.rulePackageService.getPriority(ruleAssembly.getPackageId()));
      this.ruleAssemblyService.insert(ruleAssembly);
      List<Rule> rules = this.ruleService.getRuleListByAssemblyId(id);
      Iterator var4 = rules.iterator();

      while(var4.hasNext()) {
         Rule rule = (Rule)var4.next();
         rule.setId((Long)null);
         rule.setName(rule.getName() + "" + (int)(Math.random() * 90.0D));
         rule.setRuleAssemblyId(ruleAssembly.getId());
         this.ruleService.insert(rule);
      }

      return ResultUtils.getSuccessResultData();
   }
}
